<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    /**
     * 定义主键字段
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 定义表名，不含前缀
     *
     * @var string
     */
    protected $table      = 'wd_order';

    /**
     * 定义拒绝保存的字段
     *
     * @var array
     */
    protected $guarded    = ['id'];

    /**
     * 开启laravel默认的时间记录
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * 数据类型转换
     *
     * @var array
     */
    protected $casts = [
        'total_amount'  => 'string',
        'pay_amount'    => 'string',
        'rebate_amount' => 'string',
        'return_amount' => 'string',
        'profit_price'  => 'string',
    ];

    /**
     * 一对多关系表
     * 订单对应订单商品表
     *
     * @return void
     */
    public function order_goods()
    {   
        return $this->hasMany('App\Models\OrderGoods', 'order_id', 'id');
    }

    /**
     * 远程一对多
     * 订单表对应商品表
     *
     * @return void
     */
    public function goods()
    {
        return $this->hasManyThrough('App\Models\Goods', 'App\Models\OrderGoods', 'order_id', 'id', 'id');
    }

    /**
     * 订单给商品添加图片
     *
     * @param [type] $data
     * @return void
     */
    public static function get_pic( $data, $rebate_id = null )
    {   
        foreach ($data['data'] as $k => &$v) {
            if ( $v['id'] == $rebate_id ){
                $v['is_schedule'] = 3;
            } 
            foreach ($v['order_goods'] as $k1 => &$v1) {
                
                $v1['good_pic'] = Goods::where('id', $v1['goods_id'])->value('pic');

            }
        }

        return $data;
    }

    /**
     * 支付成功的修改状态
     *pay_method  1 支付宝  2 微信  3 平台/余额
     * @return void
     */
    public static function edit_pay_order( $order_sn, $pay_method)
    {
        $info = Order::where('status', 0)->where('order_sn', $order_sn)->first();
       
        if ( !$info ) {
            return false;
        }
        
        $info->status = 1;
        $info->pay_method = $pay_method;
        $info->code = rand(1000, 9999);
        $info->save();
        
        // 给用户添加贡献值
        $user_info = User::find($info['user_id']);
        if ( $user_info ) {
            $money = (int)$info['pay_amount'];
            $user_info->rebate_points += $money / 100;
            $user_info->save();
        }

        // 给订单增加消费记录
        // UserServerBalance::insertLog($info['user_id'], $order_sn, $info['pay_amount']);
         UserServerBalance::create([
                'user_id'     => $info['user_id'],
                'order_sn'    => $order_sn,
                'money'       => $info['pay_amount'],
                'type'        => 3,
                'action_type' => 1,
                'status'      => 2,
            ]);
        return true;
    }

    /**
     * 验证订单金额
     *
     * @return void
     */
    public function check( $order_sn, $money)
    {
        $info = Order::where('status', 0)->where('order_sn', $order_sn)->where('pay_amount', $money)->first();

        return $info?true: false;
    }

    public static function countOrder( $user_id, $money = 1 )
    {
        $num = self::where('is_rebate', 1)->where('user_id', $user_id)->where('status', '>=', 1)->where('pay_amount', '>=', $money)->count();
     
        return $num;
    }
}
;